/**
 * Copyright (c) 2014- 2015 YCY Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.jkwzms.system.permission.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jkwzms.common.Constants;
import com.jkwzms.common.entity.enums.AvailableEnum;
import com.jkwzms.common.inject.annotation.BaseComponent;
import com.jkwzms.common.utils.EasyUiMsg;
import com.jkwzms.common.web.controller.BaseCRUDController;
import com.jkwzms.system.permission.entity.Permission;
import com.jkwzms.system.permission.service.PermissionService;

/**
 * <p>
 * User: Jack Liang
 * <p>
 * Date: 13-1-28 下午4:29
 * <p>
 * Version:1.0.0
 */
@Controller
@RequestMapping(value = "/sys/permission/permission")
public class PermissionController extends BaseCRUDController<Permission, Long> {

    @Autowired
    @BaseComponent
    private PermissionService permissionService;

    // public PermissionController(){
    // setResourceIdentity("sys:permission");
    // }

    @Override
    protected void setCommonData(Model model) {
        super.setCommonData(model);
        model.addAttribute("availableList", AvailableEnum.values());
    }

    /**
     * @return
     */
    @RequestMapping(value = "main", method = RequestMethod.GET)
    public String main() {
        return "sys/permission/permission/list";
    }

    /**
     * 批量修改状态 该方法目前未启用
     * 
     * @param request
     * @param newStatus
     * @param ids
     * @return
     */
    @RequestMapping(value = "/changeStatus/{newStatus}")
    public String changeStatus(HttpServletRequest request, @PathVariable("newStatus") Boolean newStatus,
                               @RequestParam("ids") Long[] ids) {

        this.permissionList.assertHasUpdatePermission();

        for (Long id : ids) {
            Permission permission = permissionService.findOne(id);
            permission.setShow(newStatus);
            permissionService.update(permission);
        }

        return "redirect:" + request.getAttribute(Constants.BACK_URL);
    }

    /**
     * 更新
     * 
     * @param model
     * @param m
     * @param result
     * @return
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> update(Model model, @Valid @ModelAttribute("m") Permission m, BindingResult result) {
        // if (permissionList != null) {
        // this.permissionList.assertHasUpdatePermission();
        // }

        if (hasError(m, result)) {
            return EasyUiMsg.successMsg(false);
        }
        this.permissionService.update(m);
        return EasyUiMsg.successMsg(true);
    }

    /**
     * @param id
     * @return
     */
    @RequestMapping(value = "get", method = RequestMethod.POST)
    @ResponseBody
    public Permission getPermissionById(@RequestParam("id") Long id) {
        return this.permissionService.findOne(id);
    }

    /**
     * 批量删除
     * 
     * @param ids
     * @return
     */
    @RequestMapping(value = "deleteByIds", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteByIds(@RequestParam("ids") Long[] ids) {
        try {
            this.permissionService.delete(ids);
            return EasyUiMsg.successMsg(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return EasyUiMsg.successMsg(false);
    }

}
